<?php
if( !defined('__ROOT__') ) exit('Load failed');
class permissionModel extends Model{
	private $table = 'permission';

	public function __construct(){
		parent::__construct($this->table);
	}

	public function add_permission($data){
		// Should do some data validation
		return $this->value($data)->add();	
	}

	public function get_all_permission(){
		return $this->get();
	}

	public function get_permission_by_id($id){
		return $this->where("id=$id")->get_one();
	}

	public function get_permission_by_group($id){
		return $this->where("user_group=$id")->get();
	}

	public function get_permission_by_task($id){
		return $this->where("task=$id")->get();
	}

	public function get_group_task_ids($id){
		$rs = $this->get_permission_by_group($id);
		$arr = array();
		if( is_array($rs) && count($rs)>0 ){
			foreach($rs as $v){
				$arr[] = $v['task'];
			}
		}
		return $arr;
	}

	public function del_permission($condition){
		if( empty($condition) ) return NULL;
		$where = "";
		if( is_array($condition) ){
			foreach($condition as $k=>$v){
				$where .= "`$k`=".htmlspecialchars($v);
			}	
		}elseif( is_string($condition) ){
			$where = $condition;
		}
		return $this->where($where)->del();	
	}
}
